package henu.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import henu.bean.Notice;
import henu.bean.Schedule;
import henu.dao.INoticeDao;

public class NoticeDaoImpl implements INoticeDao{
	protected QueryRunner qr = new QueryRunner();
	private int row = 0;
	public NoticeDaoImpl(QueryRunner qr){
		super();
		this.qr = qr; 
	}

	@Override
	public int add(Notice note) {
		String sql = "insert into tb_notice(fTitle,fContent,fPublisher,fTime) values(?,?,?,?)";
		Object[] param = {note.getfTitle(),note.getfContent(),note.getfPublisher(),note.getfTime()};
		try {
			row = qr.update(sql, param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row;
	}

	@Override
	public int delete(int id) {
		String sql = "delete from tb_notice where id=?";
		Object[] param = {id};
		try {
			row = qr.update(sql,param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row;
	}

	@Override
	public int update(Notice note) {
		String sql = "update tb_notice set fTitle=?,fContent=?,fPublisher=? where id=?";
		Object[] param = {note.getfTitle(),note.getfContent(),note.getfPublisher(),note.getId()};
		try {
			row = qr.update(sql,param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row;
	}

	@Override
	public List<Notice> select(String title) {
		String sql = "select * from tb_notice where fTitle like '%?%'";
		Object[] param = {title};
		List<Notice> notes = null;
		try {
			notes = qr.query(sql, param, new BeanListHandler<>(Notice.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return notes;
	}

	@Override
	public List<Notice> getAll() {
		String sql = "select * from tb_notice";
		List<Notice> notes = null;
		try {
			notes = qr.query(sql, new BeanListHandler<>(Notice.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return notes;
	}

	@Override
	public List<Notice> getAll(int pageSize, int pageNow) {
		String sql = "select * from tb_notice order by Id desc limit "+(pageNow*pageSize-pageSize)+","+pageSize;
		List<Notice> notes = null;
		try {
			notes = qr.query(sql, new BeanListHandler<>(Notice.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return notes;
	}
	
	@Override
	public Notice updateInfo(int id) {
		String sql = "select * from tb_notice where id = ?";
		Object[] param = {id};
		Notice note = null;
		try {
			note = qr.query(sql,param, new BeanHandler<>(Notice.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return note;
	}

	

	@Override
	public int getCount() {
		String sql = "select * from tb_notice";
		List<Notice> notes = null;
		try {
			notes = qr.query(sql, new BeanListHandler<>(Notice.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return notes.size();
	}

}
